package com.yanqu.road.server.cmd.activity.busyfarming;

import com.yanqu.road.entity.activity.busyfarming.BusyFarmingUserData;
import com.yanqu.road.entity.relatives.RelativesInfo;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.pb.activity.BusyFarmingProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.busyfarming.BusyFarmingModule;
import com.yanqu.road.server.gameplayer.module.relatives.RelativesModule;
import com.yanqu.road.server.manger.activity.busyfarming.BusyFarmingMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.date.DateHelper;

import java.util.Map;

@Cmd(code = Protocol.S_BUSY_FARMING_STEAL_SEARCH,desc = "偷菜查找")
public class BusyFarmingStealSearchCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if(BusyFarmingMgr.getShowTimeActivityId() == 0){
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        if(!BusyFarmingMgr.activityInTime()){
            ret = GameErrorCode.E_BUSY_FARMING_ACTIVITY_NO_IN_TIME;
            return;
        }

        BusyFarmingUserData userData = BusyFarmingMgr.getBusyFarmingUserData(player.getUserId());
        if(userData == null){
            ret = GameErrorCode.E_BUSY_FARMING_ACTIVITY_NO_UNLOCK;
            return;
        }

        //是否开启；需活动进行多久后开启
        int needHour = BusyFarmingMgr.getBusyFarmingOptOpenTimeParam().get(0);
        int needTIme = needHour * 3600;
        if(DateHelper.getCurrentSecond() - BusyFarmingMgr.getActivityInfo().getBeginTime() < needTIme){
            ret = GameErrorCode.E_BUSY_FARMING_ACTIVITY_STEAL_TIME_NOT_OPEN;
            return;
        }

        if(BusyFarmingMgr.isRestTime()) {
            ret = GameErrorCode.E_BUSY_FARMING_ACTIVITY_NOT_IN_OPT_TIME;
            return;
        }

        if(userData.getStealTimes() >= BusyFarmingMgr.getBusyFarmingStolenParam().get(1)){
            ret = GameErrorCode.E_BUSY_FARMING_ACTIVITY_STEAL_NO_TIMES;
            return;
        }




        //如果活动是跨服
        if(BusyFarmingMgr.isCrossBusyFarmingActivity()){
            //上跨服
            BusyFarmingProto.CrossBusyFarmingStealSearchReqMsg.Builder crossReqMsg = BusyFarmingProto.CrossBusyFarmingStealSearchReqMsg.newBuilder();
            crossReqMsg.setActivityId(BusyFarmingMgr.getShowTimeActivityId());
            crossReqMsg.setGrade(userData.getHomeGrade());
            crossReqMsg.setUnionUid(player.getUserInfo().getUnionUid());
            crossReqMsg.setGradeRange(BusyFarmingMgr.getBusyFarmingStolenParam().get(0));
            //亲家
            Map<Long, RelativesInfo> relativesInfoMap = player.getModule(RelativesModule.class).getRelativesMap();
            for(Map.Entry<Long, RelativesInfo> entry : relativesInfoMap.entrySet()){
                RelativesInfo relativesInfo = entry.getValue();
                if(relativesInfo.getStatus() == 1) {
                    crossReqMsg.addFriend(entry.getKey());
                }
            }
            //偷取玩家
            crossReqMsg.addAllStolenUser(player.getModule(BusyFarmingModule.class).getStolenUser());
            //盟友
            Map<Long, CrossUnionMember> memberMap = CrossUnionMgr.getUnionMemberMap(player.getUserInfo().getUnionUid());
            if(memberMap != null){
                //crossReqMsg.addAllFriend(memberMap.keySet());
            }
            player.sendPacket(Protocol.C_BUSY_FARMING_ACTIVITY_STEAL_SEARCH, crossReqMsg);
        }else {
            //本服活动
            ret = player.getModule(BusyFarmingModule.class).stealSearch();
        }
    }

    @Override
    public void sendData(GamePlayer player) {
       if(ret != 0){
           BusyFarmingProto.CrossBusyFarmingStealSearchRespMsg.Builder respMsg = BusyFarmingProto.CrossBusyFarmingStealSearchRespMsg.newBuilder();
           respMsg.setRet(ret);
           respMsg.setCsrf(0);//temp
           respMsg.setActivityId(BusyFarmingMgr.getShowTimeActivityId());
           player.sendPacket(Protocol.U_BUSY_FARMING_STEAL_SEARCH, respMsg);
       }
    }
}
